package com.xq.shop.controller.admin;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.xq.shop.po.Menu;
import com.xq.shop.po.Role;
import com.xq.shop.service.admin.IBaseService;
import com.xq.shop.service.admin.IMenuService;
import com.xq.shop.service.admin.IRoleService;
import com.xq.shop.util.AdminServiceFactory;
import com.xq.shop.vo.RespMsg;
import com.xq.shop.vo.SearchObject;
import com.xq.shop.vo.RespMsg.Result;

@Controller
@RequestMapping("/admin/")
public class RoleController extends BaseController<Role>{

	IRoleService roleService = AdminServiceFactory.getRoleService();
	
	IMenuService menuService = AdminServiceFactory.getMenuService();
	
	@Override
	public IBaseService<Role> getService() {
		return roleService;
	}
	
	@RequestMapping("role_list.do")
	@ResponseBody
	public String list(SearchObject search, ModelMap model){
		
		return super.list(search, new ExclusionStrategy() {
			
			@Override
			public boolean shouldSkipClass(Class<?> clazz) {
				return false;
			}

			@Override
			public boolean shouldSkipField(FieldAttributes f) {
				return "menus".equals(f.getName());
			}
		});
	}
	
	@RequestMapping("role_delete.do")
	public void delete(String id, HttpServletResponse response){
		
		super.delete(id, response);
	}
	
	@RequestMapping("role_edit")
	public ModelAndView edit(String id){
		
		ModelAndView modelAndView = new ModelAndView("admin/setting/role_edit");
		Role role;
		if(id != null){
			role = roleService.get(Long.parseLong(id));
		} else {
			role = new Role();
		}
		
		if(role == null){
			return modelAndView;
		} else {
			Map<Menu, List<Menu>> menuMap = menuService.getMenuTree(0L);
			modelAndView.addObject(menuMap);
			modelAndView.addObject(role);
			
			Set<Menu> menus = role.getMenus();
			if(menus != null && menus.size() > 0){
				
				String privilege = "";
				Iterator<Menu> it = menus.iterator();
				while(it.hasNext()){
					
					privilege += it.next().getId() + ",";
				}
				modelAndView.addObject("selected", privilege.substring(0, privilege.length() - 1));
			}
			return modelAndView;
		}
	}
	
	@RequestMapping("role_save.do")
	public ModelAndView save(Role role, Long[] menuId, HttpServletResponse response) {
		
		if(menuId != null && menuId.length > 1){
			List<Menu> menus = menuService.get(menuId);
			if(menus != null){
				
				role.setMenus(new HashSet<Menu>(menus));
			}
		}
		
		roleService.saveOrUpdate(role);
		ModelAndView modelAndView = new ModelAndView("admin/setting/role_list");
		modelAndView.addObject(new RespMsg(Result.success, "保存成功"));
		return modelAndView;
	}
}
